package com.oig.sys.msg.dao;

import com.oig.sys.msg.entity.TbNoticeMsgReceiver;
import com.oig.sys.msg.vo.NoticeMsgReceiverVo;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;

/**
@author wuxl
*/
public interface ITbNoticeMsgReceiverDao extends JpaRepository<TbNoticeMsgReceiver, Integer> {

	@Query("select new com.oig.sys.msg.vo.NoticeMsgReceiverVo(r.receiverId, r.receiverName) from TbNoticeMsgReceiver r where r.msgId=:msgId")
	List<NoticeMsgReceiverVo> findAllByMsgId(@Param("msgId")Integer msgId);

	@Query("select r.id from TbNoticeMsgReceiver r where r.msgId=:msgId")
	List<Integer> findAllIdByMsgId(@Param("msgId") Integer msgId);

	@Query("select r.id from TbNoticeMsgReceiver r where r.msgId=:msgId and r.id not in(:idList)")
	List<Integer> findAllDelId(@Param("msgId") Integer msgId, @Param("idList") List<Integer> idList);

	@Query("select r.id from TbNoticeMsgReceiver r where r.receiverId=:userId and r.msgStatus='01'")
	List<Integer> findAllUnReadId(@Param("userId") Integer userId);

	@Query("select r.id from TbNoticeMsgReceiver r where r.receiverId=:userId and r.delFlag='0'")
	List<Integer> findAllUnDelId(@Param("userId") Integer userId);

	@Query("select r from TbNoticeMsgReceiver r where r.msgId=:msgId and r.receiverId=:userId ")
	TbNoticeMsgReceiver getByMsgIdAndReceiverId(@Param("msgId") Integer msgId, @Param("userId") Integer userId);


    @Query("select r from TbNoticeMsgReceiver r where r.receiverId=:userId and r.msgId in(:msgIdList)")
	List<TbNoticeMsgReceiver> findByMsgIdAndReceiverId(@Param("msgIdList") List<Integer> msgIdList, @Param("userId") Integer userId);


	@Modifying
	@Query("delete from  TbNoticeMsgReceiver r where r.id in(:idList)")
	void deleteByIdList(@Param("idList") List<Integer> idList);


	/**
	 * 02 已读
	 */
    @Modifying
    @Query("update TbNoticeMsgReceiver r set r.msgStatus='02' where r.id in(:idList)")
    void updateUserRead(@Param("idList") List<Integer> idList);

	/**
	 * 1 删除
	 */
    @Modifying
    @Query("update TbNoticeMsgReceiver r set r.delFlag='1' where r.id in(:idList)")
    void updateUserDel(@Param("idList") List<Integer> idList);

}
